﻿using System;
using System.Linq;
using db = DataContext;

namespace WebApplication.admin
{
	public partial class BigPictrue : System.Web.UI.Page
	{
		public bool isadd, isdelete, issave, isedit;
		public int id;

		protected void Page_Load(object sender, EventArgs e)
		{
			Common.CheckLogin();

			isadd = Common.IsAdd;
			isdelete = Common.IsDelete;
			issave = Common.IsSave;
			isedit = Common.IsEdit;

			id = Common.Id;

			using (db.DbDataContext context = new db.DbDataContext())
			{
				if (issave)
				{
					try
					{
						string guid = Guid.NewGuid().ToString();
						Request.Files["file"].SaveAs(Server.MapPath("~/upload/screen/" + guid + ".jpg"));

						context.IndexPictures.InsertOnSubmit(new db.IndexPictures
						{
							GUID = guid,
							Sort = 0
						});
						context.SubmitChanges();
						Response.Redirect("bigpictrue.aspx");
					} catch (Exception ex)
					{
						Response.Write(ex.Message);
					}
				}

				if (isdelete)
				{
					if (context.IndexPictures.Count() > 1)
					{
						context.IndexPictures.DeleteOnSubmit(context.IndexPictures.Single(x => x.Id == id));
						context.SubmitChanges();
					}
					Response.Redirect("bigpictrue.aspx");
				}

				if (isedit)
				{
					foreach (string item in Request.Form)
					{
						if (item.StartsWith("i_"))
						{
							context.IndexPictures.Single(x => x.Id == int.Parse(item.Substring(2))).Sort = int.Parse(Request.Form[item]);
						}
					}
					context.SubmitChanges();
					Response.Redirect("bigpictrue.aspx");
				}

				i_list.DataSource = context.IndexPictures.OrderBy(x => x.Sort);
				i_list.DataBind();
			}
		}
	}
}